package com.zf.shuashua.common;

import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.List;


public class PhoneCallManager {

    List<CallListener> mCallListeners = new ArrayList<>();

    Intent mPhoneCallService;

    private PhoneCallManager() {
    }

    private static class SingletonHolder {
        private static final PhoneCallManager INSTANCE = new PhoneCallManager();
    }

    public static PhoneCallManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void startService(Context context) {
        try {
            if (mPhoneCallService != null) {
                stopService(context);
            }
            mPhoneCallService = new Intent();
            mPhoneCallService.setClass(context, PhoneCallService.class);
            context.startService(mPhoneCallService);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopService(Context context) {
        try {
            if (mPhoneCallService != null) {
                context.stopService(mPhoneCallService);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mPhoneCallService = null;
    }

    public void addCallListener(CallListener callListener) {
        this.mCallListeners.add(callListener);
    }

    public void deleteCallListener(CallListener callListener) {
        this.mCallListeners.remove(callListener);
    }

    public void onCallRinging() {
        for (CallListener callListener : mCallListeners) {
            callListener.onCallRinging();
        }
    }

    public void onCallOffHook() {
        for (CallListener callListener : mCallListeners) {
            callListener.onCallOffHook();
        }
    }

    public void onCallIdle() {
        for (CallListener callListener : mCallListeners) {
            callListener.onCallIdle();
        }
    }

    public interface CallListener {

        void onCallRinging();

        void onCallOffHook();

        void onCallIdle();
    }

}